package pl.edu.pw.polygen.modeler.client.properties;

import java.util.List;

import pl.edu.pw.polygen.modeler.client.object.IPolygenObject;
import pl.edu.pw.polygen.modeler.client.object.Segment;
import pl.edu.pw.polygen.modeler.client.object.Splain;
import pl.edu.pw.polygen.modeler.client.object.Vertex;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;

public class Properties extends Composite implements Paintable{

	private VerticalPanel panel;
	private VertexProperties vertexProperties;
	private SegmentProperties segmentProperties;
	private SplainProperties splainProperties;
	private BoundaryProperties boundaryProperties;
	
	public Properties(){
		this.initialize();
	}
	
	private void initialize(){
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("160px");
		vertexProperties = new VertexProperties();
		segmentProperties = new SegmentProperties();
		splainProperties = new SplainProperties();
		boundaryProperties = new BoundaryProperties();
		Label propertiesLabel = new Label("Properties");
		propertiesLabel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		propertiesLabel.setHeight("20px");
		panel.add(propertiesLabel);
		panel.add(vertexProperties);
		panel.add(segmentProperties);
		panel.add(splainProperties);
		panel.add(boundaryProperties);
		this.hideAll();
		initWidget(panel);
	}
	
	public void showProperty(IPolygenObject polygenObject){
		if(polygenObject instanceof Vertex){
			showVertex((Vertex)polygenObject);
		}else if(polygenObject instanceof Segment){
			showSegment((Segment) polygenObject);
		}else if(polygenObject instanceof Splain){
			showSplain((Splain)polygenObject);
		}
	}
	
	public void showProperty(List<IPolygenObject> polygenObjects){
		showBoundaryProperties(polygenObjects);
	}
	
	private void showBoundaryProperties(List<IPolygenObject> polygenObjects) {
		hideAll();
		boundaryProperties.setVisible(true);
		boundaryProperties.setModel(polygenObjects);
	}

	private void showVertex(Vertex vertex){
		hideAll();
		vertexProperties.setVisible(true);
		vertexProperties.setVertex(vertex);
	}
	
	private void showSegment(Segment segment){
		hideAll();
		segmentProperties.setVisible(true);
		segmentProperties.setSegment(segment);
	}
	
	private void showSplain(Splain splain){
		hideAll();
		splainProperties.setVisible(true);
		splainProperties.setSplain(splain);
	}
	
	private void hideAll(){
		vertexProperties.setVisible(false);
		segmentProperties.setVisible(false);
		splainProperties.setVisible(false);
		boundaryProperties.setVisible(false);
	}
	
	@Override
	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		vertexProperties.updateFromUIDL(uidl, client);
		segmentProperties.updateFromUIDL(uidl, client);
		splainProperties.updateFromUIDL(uidl, client);
		boundaryProperties.updateFromUIDL(uidl, client);
	}
}
